package cn.cstengxun.springboot.deng.controller;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @ClassName SendPhoneController
 * @Description 手机号短信验证码
 * @Author Mr.Deng
 * @Date 2023/11/29 19:35
 * @Version 1.0
 */
@RestController
@RequestMapping("/phone")
public class SendPhoneController {

    @Value("${aliyun.ALIBABA_CLOUD_ACCESS_KEY_ID}")
    private String ALIBABA_CLOUD_ACCESS_KEY_ID;

    @Value("${aliyun.ALIBABA_CLOUD_ACCESS_KEY_SECRET}")
    private String ALIBABA_CLOUD_ACCESS_KEY_SECRET;

    @Value("${aliyun.signName}")
    private String signName;

    @RequestMapping("/")
    public void doSend() {

        Map<String, String> map = new HashMap<>();
        map.put("region", "cn-hangzhou");           //服务器地址
        map.put("phoneNumbers", "19189708061");     //需要收到信息的手机号
        map.put("templateCode", "SMS_464050868");   //阿里云中模板码
        map.put("templateParam", "{\"code\":\"4651\"}"); //信息内容参数
        //发短信
        send(map);

    }


    private void send(Map<String, String> map) {
        // 配置凭据认证信息，包括ak、secret、token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // 请确保设置了环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET.
                //将值设在application.yml中以便维修及找出问题
                .accessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
                .accessKeySecret(ALIBABA_CLOUD_ACCESS_KEY_SECRET)
                .build());

        // 配置客户端
        AsyncClient client = AsyncClient.builder()
                .region(map.get("region")) // 服务器区域ID
                // 使用配置好的HttpClient，否则使用默认的HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                // 重写客户端级配置，可以设置端点、Http请求参数等。
                .overrideConfiguration(
                        ClientOverrideConfiguration.create().setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        // API请求的参数设置
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(map.get("phoneNumbers"))
                .signName(signName)
                .templateCode(map.get("templateCode"))
                .templateParam(map.get("templateParam"))
                // 请求级配置重写，可以设置Http请求参数等
                .build();

        // 异步获取API请求的返回值
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);

        // 同步获取API请求的返回值
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 通过GSON将响应转换成JSON格式，如果使用FastJson可写成：
        System.out.println(new Gson().toJson(resp));

        // 最后关闭客户端
        client.close();
    }

}
